package com.lianniu.mgr.login;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.baseService.staff.model.StaffVo;
import com.lianniu.mgr.baseService.staff.service.IStaffService;
import com.lianniu.mgr.constants.ErrorCode;
import com.lianniu.mgr.constants.UserErrorCode;
import com.lianniu.mgr.util.MD5Util;
import com.lianniu.mgr.util.StringUtil;
import com.lianniu.mgr.util.UUIDUtil;
import com.lianniu.redis.RedisUtils;



/**
 * 
 * @ClassName: LoginFilterController
 * @Description: TODO

 * @date: 2016年3月16日 下午7:23:24
 */
@Controller
@RequestMapping("/rest")
public class LoginController {
  private final static Logger logger = Logger.getLogger(LoginController.class);
  private final static int TIME=3600*24;
  
  @Resource
  IStaffService staffService;

  @RequestMapping(value = {"/login"}, method = RequestMethod.POST)
  public @ResponseBody BaseResult userCheckFail(HttpServletRequest request, String username,
      String userpasswd, String codeUuid, String code,int times) {
    logger.info("LoginController userCheckFail begin.");
    logger.info("parameter username:" + username);
    logger.info("parameter userpasswd:" + userpasswd);
    logger.info("parameter codeUuid:" + codeUuid);
    logger.info("parameter code:" + code);
    logger.info("parameter times:" + times);
    
    logger.info("userCheckFail checkParameter begin.");
    if (StringUtil.isEmpty(username)) {
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    if (StringUtil.isEmpty(userpasswd)) {
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    if (StringUtil.isEmpty(codeUuid)) {
      return BaseResult.fail(ErrorCode.IsNullRetCode, ErrorCode.IsNullRetInfo);
    }
    
    
    RedisUtils redisUtils = new RedisUtils();
    if(times>3){
      if (StringUtil.isEmpty(code)) {
        return BaseResult.fail(ErrorCode.IsNullRetCode, ErrorCode.IsNullRetInfo);
      }
      
      if (!redisUtils.get("codeUuid:" + codeUuid).equalsIgnoreCase(code)) {
        return BaseResult.fail(UserErrorCode.CaptchaErrorRetCode, UserErrorCode.CaptchaErrorRetInfo);
      }
    }
    logger.info("userCheckFail checkParameter end.");


    StaffVo staffVo = new StaffVo();
    staffVo.setLoginName(username);
    logger.info("userCheckFail getStaff begin.");
    BaseResult baseResult = staffService.getStaff(staffVo);
    logger.info("userCheckFail getStaff end.");
    if(!baseResult.getRetCode().equals(ErrorCode.SuccessRetCode)){
      return baseResult;
    }
    
    logger.info("userCheckFail checkuserpasswd begin.");
    StaffVo retStaffVo = (StaffVo) baseResult.getData();
    if (!MD5Util.checkPassword(userpasswd, retStaffVo.getLoginPassword())
        || !retStaffVo.getLoginName().equals(username)) {
      return BaseResult.fail(UserErrorCode.UserOrPassErrorRetCode,
          UserErrorCode.UserOrPassErrorRetInfo);
    }
    logger.info("userCheckFail checkuserpasswd end.");

    //delete the old token
//    String oldtoken=redisUtils.get("saasMgr:"+username);
//    redisUtils.del("saasMgr:" + oldtoken);
    //update new token
    String token = UUIDUtil.getUUID();
    redisUtils.set("saasMgr:" + token, username);
    redisUtils.set("saasMgr:"+username, token);
//    redisUtils.setTimeOut("saasMgr:" + token, username, TIME);
//    redisUtils.setTimeOut("saasMgr:"+username, token, TIME);
    
    redisUtils.expire("saasMgr:" + token, TIME);
    redisUtils.expire("saasMgr:" + username, TIME);
    
    Map<String, String> result = new HashMap<String, String>();
    result.put("token", token);
    result.put("username", retStaffVo.getLoginName());
    result.put("userid", retStaffVo.getStaffId());
    logger.info("userCheckFail return:"+JSON.toJSONString(result));
    logger.info("LoginController userCheckFail end.");
    return BaseResult.success(result);

  }


  @RequestMapping(value = {"/getMemuList"}, method = RequestMethod.GET)
  public @ResponseBody BaseResult getMemuList(HttpServletRequest request, String staffid,
      String sysinfo_id) {


    return BaseResult.success();
  }
}
